package com.sortingalgorithm;

import java.util.Arrays;

/*
* 快速排序
* */
public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {1,3,6,7,2,54,54,87,0,4};
        quickSort(arr,0,arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }


    public static void quickSort(int[] arr,int low,int high){
        if(low >= high){
            return;
        }
        int index = subQuickSort(arr,low,high);
        // 左边快排
        quickSort(arr,low,index - 1);
        // 右边快排
        quickSort(arr,index+1,high);

    }

    private static int subQuickSort(int[] arr,int left,int right) {

        // 枢轴
        int pivot = arr[left];
        int l = left;
        int r = right;
        while (l < r){
            while (l < r && arr[r] >= pivot){
                r--;
            }
            arr[l] = arr[r];
            while (l < r && arr[l] <= pivot ){
                l++;
            }
            arr[r] = arr[l];
        }
        arr[l] = pivot;

        return l ;

    }

}
